Method of improving a query to a database system

ABSTRACT

In embodiments, methods and systems for improving a query in a database system are provided. These method and system embodiments can enable greater contextual knowledge about the types and use of data in tables underlying a relational database to be employed to improve query efficiency. By employing contextual information, embodiments can provide improved queries and/or make recommendations to a query optimizer of a database system to improve its operation based upon knowledge of the data and/or application gathered. Embodiments can be useful in improving query performance in multi-tenant database systems.

CROSS-REFERENCES TO RELATED APPLICATIONS

This application is a continuation in part application of co-pending U.S. patent application Ser. No. 10/669,523 filed Sep. 23, 2003, entitled “QUERY OPTIMIZATION IN A MULTI-TENANT DATABASE SYSTEM,” the entire disclosure of which is incorporated by reference for all purposes. The present application is also related to co-pending U.S. patent application Ser. No. 10/817,161 filed Apr. 2, 2004, entitled “CUSTOM ENTITIES AND FIELDS IN A MULTI-TENANT DATABASE SYSTEM,” the entire disclosure of which is incorporated by reference for all purposes.

COPYRIGHT NOTICE

A portion of the disclosure of this patent document contains material which is subject to copyright protection. The copyright owner has no objection to the facsimile reproduction by anyone of the patent document or the patent disclosure, as it appears in the Patent and Trademark Office patent file or records, but otherwise reserves all copyright rights whatsoever.

FIELD OF THE INVENTION

The present invention relates generally to database systems and more particularly to query optimization systems and methods for use in multi-tenant database systems.

BACKGROUND

In database systems, to access, retrieve and process stored data, a query is generated, automatically or manually, in accordance with the application program interface protocol for the database. In the case of a relational database, the standard protocol is the structured query language (SQL). SQL statements are used both for interactive queries for data from the database and for gathering data and statistics. The efficiency of the query method underlying the actual query is dependent in part on the size and complexity of the data structure scheme of the database and in part on the query logic used.

Conventionally, query optimizers can be used on any database, such as a relational database provided by Oracle™, a company with headquarters in Redwood Shores, Calif. Such query optimizers work generally as follows: for each table, column, or index, aggregate statistics are gathered (typically periodically or on demand by a database administrator (“DBA”)). The gathered statistics typically include the total number of rows, average size of rows, total number of distinct values in a column or index (an index can span multiple columns), histograms of column values (which place a range of values into buckets), etc. The optimizer then uses these statistics to decide among a possible set of data access paths.

However, such conventional query optimizers fail when presented with situations such as when data is not homogeneously distributed throughout the database, because the optimizer is unaware that for specific columns the data may have different characteristics.

In the case of table joins, the optimizer's decisions may be even more important—deciding which table to retrieve first can have a profound impact on overall query performance. Here again, by using system-wide aggregate statistics the optimizer might choose a query plan that is incorrect or inefficient when confronted with data that does not conform to the “normal” average of the entire database as determined from the gathered statistics.

Accordingly, it is desirable to provide systems and methods for improving database queries which overcome the above and other problems.

BRIEF SUMMARY

The present invention provides methods and systems for improving a query in a database system. These method and system embodiments can enable greater contextual knowledge about the types and use of data in tables underlying a relational database to be employed to improve query efficiency. By employing contextual information, embodiments can provide improved queries and/or make recommendations to a query optimizer of a database system to improve its operation based upon knowledge of the data and/or application gathered. Embodiments can be useful in improving query performance in multi-tenant database systems.

As used herein, the term multi-tenant database system refers to those systems in which various elements of hardware and software of the database system may be shared one or more customers. For example, a given application server may simultaneously process requests for a great number of customers, and a given database table may store rows for a potentially much greater number of customers. As used herein, the term contextual information refers broadly to any information about the distribution or allocation of information in an underlying database, or the effects of specific operations on the data, including permissions by one or more tenants to access specific data or to perform specific operations in a multi-tenant database system.

In an aspect and by way of example, the present invention provides a method for improving a query in a database that includes receiving a query directed to the database. Queries may be intercepted prior to reaching a relational database management system (RDBMS) associated with the database, and before reaching an optimizer of the RDBMS if the RDBMS is so equipped. The database stores tenant specific data such that at least two of the tenants may store at least a portion of tenant specific data into a common table within the database in a multi-tenant database implementation. In place of the query, an improved query is provided. The improved query is determined based at least in part upon the query received and a set of contextual information. The contextual information describes a characteristic of the data specific to at least one of a plurality of tenants. This can enable embodiments to construct the improved query to be aware of the data specific to a tenant to which the improved query will be addressed.

Reference to the remaining portions of the specification, including the drawings and claims, will realize other features and advantages of the present invention. Further features and advantages of the present invention, as well as the structure and operation of various embodiments of the present invention, are described in detail below with respect to the accompanying drawings. In the drawings, like reference numbers indicate identical or functionally similar elements.

BRIEF DESCRIPTION OF THE DRAWINGS

FIG. 1 illustrates an environment wherein a multi-tenant database system (MTS) might be used in an embodiment.

FIG. 2 illustrates elements of an MTS and interconnections therein in an embodiment.

FIG. 3 illustrates an example of objects represented as a main table in an embodiment.

FIG. 4 illustrates an example Many-to-Many (MTM) physical table capable of storing sharing information in an embodiment.

FIG. 5 illustrates an example search name lookup (SNL) table in an embodiment.

FIG. 6 illustrates an example custom field table in an embodiment.

FIG. 7 illustrates an example of a custom object represented as a custom field table including physical index columns in an embodiment.

FIG. 8 illustrates an example le of a custom entity object including a custom table for a hypothetical company in an embodiment.

DETAILED DESCRIPTION

I. Multi-Tenant Database Overview

In embodiments, techniques for improving queries for databases can overcome deficiencies of conventional database query methods that have been inefficient for multi-tenant databases because such methods fail to account for certain characteristics of each tenant's data in a multi-tenant database organization. For example, while one tenant's data may include numerous short records having only one or two indexable fields, another tenant's data may include fewer, longer records having numerous indexable fields. Additionally, embodiments can provide improved queries that are custom entity and/or custom field aware, to meet the requirements of tenants that create such custom entities or custom fields, as described in co-pending U.S. patent application Ser. No. 10/817,161, incorporated by reference herein in its entirety.

In addition to structural (schema) differences, the distribution of data among different tenants in a multi-tenant database may be quite different, even when tenant schemas are similar. Modem relational databases rely on statistics-based query optimizers that make decisions about the best manner to answer a query given accurate table-level and column-level statistics that are gathered periodically. Importantly, however, because existing relational databases are not multi-tenant aware, these statistics cut across all tenants in the database. That is, the statistics that are gathered are not specific to any one tenant, but are in fact an aggregate or average of all tenants. This approach can lead to incorrect assumptions and query plans about any one tenant.

In general, one goal of a query optimizer is to minimize the amount of data that must be read from disk (e.g., because disk access may be a slow operation). The optimizer therefore typically chooses tables or columns that are most “selective”—that is, will yield the fewest rows when the query condition is evaluated. For instance, if a single query filters on two columns of a single table, and both columns are indexed, then the optimizer will use the index that has the highest number of distinct values because statistically for any given filter value a smaller number of rows are expected to be returned. If the optimizer determines that a certain column has a very high cardinality (number of distinct values) then the optimizer will choose to use an index on that column versus a similar index on a lower cardinality column. Conventionally, the optimizer assumes relatively even distribution of data and therefore reaches the conclusion that the high-cardinality column is likely to yield a smaller number of satisfying-rows for a given equality filter. Next, mechanisms and methods for providing improved queries to databases that overcome these and other shortcomings of conventional query optimizers will be described with reference to example embodiments.

FIG. 1 illustrates an environment wherein a multi-tenant database system might be used. As illustrated in FIG. 1 (and in more detail in FIG. 2) any user systems 12 might interact via a network 14 with a multi-tenant database system (MTS) 16. The users of those user systems 12 might be users in differing capacities and the capacity of a particular user system 12 might be entirely determined by the current user. For example, where a salesperson is using a particular user system 12 to interact with MTS 16, that user system has the capacities allotted to that salesperson. However, while an administrator is using that user system to interact with MTS 16, that user system has the capacities allotted to that administrator.

Network 14 can be a LAN (local area network), WAN (wide area network), wireless network, point-to-point network, star network, token ring network, hub network, or other configuration. As the most common type of network in current use is a TCP/IP (Transfer Control Protocol and Internet Protocol) network such as the global internetwork of networks often referred to as the “Internet” with a capital “I,” that will be used in many of the examples herein, but it should be understood that the networks that the present invention might use are not so limited, although TCP/IP is the currently preferred protocol.

User systems 12 might communicate with MTS 16 using TCP/IP and, at a higher network level, use other common Internet protocols to communicate, such as HTTP, FTP, AFS, WAP, etc. As an example, where HTTP is used, user system 12 might include an HTTP client commonly referred to as a “browser” for sending and receiving HTTP messages from an HTTP server at MTS 16. Such HTTP server might be implemented as the sole network interface between MTS 16 and network 14, but other techniques might be used as well or instead. In some implementations, the interface between MTS 16 and network 14 includes load sharing functionality, such as round-robin HTTP request distributors to balance loads and distribute incoming HTTP requests evenly over a plurality of servers. Preferably, each of the plurality of servers has access to the MTS's data, at least as for the users that are accessing that server.

In aspects, the system shown in FIG. 1 implements a web-based customer relationship management (CRM) system. For example, in one aspect, MTS 16 can include application servers configured to implement and execute CRM software applications as well as provide related data, code, forms, web pages and other information to and from user systems 12 and to store to, and retrieve from, a database system related data, objects and web page content. With a multi-tenant system, tenant data is preferably arranged so that data of one tenant is kept separate from that of other tenants so that one tenant does not have access to another's data, unless such data is expressly shared.

One arrangement for elements of MTS 16 is shown in FIG. 1, including a network interface 20, storage 22 for tenant data, storage 24 for system data accessible to MTS 16 and possibly multiple tenants, program code 26 for implementing various functions of MTS 16, and a process space 28 for executing MTS system processes and tenant-specific processes, such as running applications as part of an application service.

Some elements in the system shown in FIG. 1 include conventional, well-known elements that need not be explained in detail here. For example, each user system 12 could include a desktop personal computer, workstation, laptop, PDA, cell phone, or any WAP-enabled device or any other computing device capable of interfacing directly or indirectly to the Internet or other network connection. User system 12 typically runs an HTTP client, e.g., a browsing program, such as Microsoft's Internet Explorer™ browser, Netscape's Navigator™ browser, Opera's browser, or a WAP-enabled browser in the case of a cell phone, PDA or other wireless device, or the like, allowing a user (e.g., subscriber of a CRM system) of user system 12 to access, process and view information and pages available to it from MTS 16 over network 14. Each user system 12 also typically includes one or more user interface devices, such as a keyboard, a mouse, touch screen, pen or the like, for interacting with a graphical user interface (GUI) provided by the browser on a display (e.g., monitor screen, LCD display, etc.) in conjunction with pages, forms and other information provided by MTS 16 or other systems or servers. As discussed above, the present invention is suitable for use with the Internet, which refers to a specific global internetwork of networks. However, it should be understood that other networks can be used instead of the Internet, such as an intranet, an extranet, a virtual private network (VPN), a non-TCP/IP based network, any LAN or WAN or the like.

According to one embodiment, each user system 12 and all of its components are operator configurable using applications, such as a browser, including computer code run using a central processing unit such as an Intel Pentium processor or the like. Similarly, MTS 16 (and additional instances of MTS'S, where more than one is present) and all of their components might be operator configurable using application(s) including computer code run using a central processing unit such as an Intel Pentium processor or the like, or multiple processor units. Computer code for operating and configuring MTS 16 to intercommunicate and to process web pages and other data and media content as described herein is preferably downloaded and stored on a hard disk, but the entire program code, or portions thereof, may also be stored in any other volatile or non-volatile memory medium or device as is well known, such as a ROM or RAM, or provided on any media capable of storing program code, such as a compact disk (CD) medium, digital versatile disk (DVD) medium, a floppy disk, and the like. Additionally, the entire program code, or portions thereof, may be transmitted and downloaded from a software source, e.g., over the Internet, or from another server, as is well known, or transmitted over any other conventional network connection as is well known (e.g., extranet, VPN, LAN, etc.) using any communication medium and protocols (e.g., TCP/IP, HTTP, HTTPS, Ethernet, etc.) as are well known. It will also be appreciated that computer code for implementing aspects of the present invention can be implemented in any programming language that can be executed on a server or server system such as, for example, in C, C++, HTML, Java, JavaScript, any other scripting language, such as VBScript and many other programming languages as are well known.

According to one embodiment, each MTS 16 is configured to provide web pages, forms, data and media content to user systems 12 to support the access by user systems 12 as tenants of MTS 16. As such, MTS 16 provides security mechanisms to keep each tenant's data separate unless the data is shared. If more than one MTS is used, they may be located in close proximity to one another (e.g., in a server farm located in a single building or campus), or they may be distributed at locations remote from one another (e.g., one or more servers located in city A and one or more servers located in city B). As used herein, each MTS could include one or more logically and/or physically connected servers distributed locally or across one or more geographic locations. Additionally, the term “server” is meant to include a computer system, including processing hardware and process space(s), and an associated storage system and database application (e.g., RDBMS) as is well known in the art. It should also be understood that “server system” and “server” are often used interchangeably herein. Similarly, the databases described herein can be implemented as single databases, a distributed database, a collection of distributed databases, a database with redundant online or offline backups or other redundancies, etc., and might include a distributed database or storage network and associated processing intelligence.

FIG. 2 illustrates elements of MTS 16 and various interconnections in an embodiment. In this example, the network interface is implemented as one or more HTTP application servers 100. Also shown is system process space 102 including individual tenant process spaces 104, a system database 106, tenant database(s) 108 and a tenant management process space 110. Tenant database 108 might be divided into individual tenant storage areas 112, which can be either a physical arrangement or a logical arrangement. Within each tenant storage area 112, user storage 114 might similarly be allocated for each user.

It should also be understood that each application server 100 may be communicably coupled to database systems, e.g., system database 106 and tenant database(s) 108, via a different network connection. For example, one server 100 ₁ might be coupled via the Internet 14, another server 100 _(N-1) might be coupled via a direct network link, and another server 100 _(N) might be coupled by yet a different network connection. Transfer Control Protocol and Internet Protocol (TCP/IP) are preferred protocols for communicating between servers 100 and the database system, however, it will be apparent to one skilled in the art that other transport protocols may be used to optimize the system depending on the network interconnect used.

In aspects, each application server 100 is configured to handle requests for any user/organization. Because it is desirable to be able to add and remove application servers from the server pool at any time for any reason, there is preferably no server affinity for a user and/or organization to a specific application server 100. In one embodiment, therefore, an interface system (not shown) implementing a load balancing function (e.g., an F5 Big-IP load balancer) is communicably coupled between the servers 100 and the user systems 12 to distribute requests to the servers 100. In one aspect, the load balancer uses a least connections algorithm to route user requests to the servers 100. Other examples of load balancing algorithms, such as round robin and observed response time, also can be used. For example, in certain aspects, three consecutive requests from the same user could hit three different servers, and three requests from different users could hit the same server. In this manner, MTS 16 is multi-tenant, wherein MTS 16 handles storage of different objects and data across disparate users and organizations.

As an example of storage, one tenant might be a company that employs a sales force where each salesperson uses MTS 16 to manage their sales process. Thus, a user might maintain contact data, leads data, customer follow-up data, performance data, goals and progress data, etc., all applicable to that user's personal sales process (e.g., in tenant database 108). In one MTS arrangement, since all of this data and the applications to access, view, modify, report, transmit, calculate, etc., can be maintained and accessed by a user system having nothing more than network access, the user can manage his or her sales efforts and cycles from any of many different user systems. For example, if a salesperson is visiting a customer and the customer has Internet access in their lobby, the salesperson can obtain critical updates as to that customer while waiting for the customer to arrive in the lobby.

While each user's sales data might be separate from other users' sales data regardless of the employers of each user, some data might be organization-wide data shared or accessible by a plurality of users or all of the sales force for a given organization that is a tenant. Thus, there might be some data structures managed by MTS 16 that are allocated at the tenant level while other data structures might be managed at the user level. Because an MTS might support multiple tenants including possible competitors, the MTS should have security protocols that keep data, applications and application use separate. Also, because many tenants will opt for access to an MTS rather than maintain their own system, redundancy, up-time and backup are more critical functions and need to be implemented in the MTS.

In addition to user-specific data and tenant-specific data, MTS 16 might also maintain system level data usable by multiple tenants or other data. Such system level data might include industry reports, news, postings, and the like that are sharable among tenants.

In certain aspects, client systems 12 communicate with application servers 100 to request and update system-level and tenant-level data from MTS 16 that may require one or more queries to database system 106 and/or database system 108. MTS 16 (e.g., an application server 100 in MTS 16) generates automatically one or more SQL statements (the SQL query) designed to access the desired information.

Each database can generally be viewed as a collection of objects, such as a set of logical tables, containing data fitted into predefined categories. A “table” is one representation of a data object, and is used herein to simplify the conceptual description of objects and custom objects according to the present invention. It should be understood that “table” and “object” may be used interchangeably herein. Each table generally contains one or more data categories logically arranged as columns or fields in a viewable schema. Each row or record of a table contains an instance of data for each category defined by the fields. For example, a CRM database may include a table that describes a customer with fields for basic contact information such as name, address, phone number, fax number, etc. Another table might describe a purchase order, including fields for information such as customer, product, sale price, date, etc. In some multi-tenant database systems, standard entity tables might be provided. For CRM database applications, such standard entities might include tables for Account, Contact, Lead and Opportunity data, each containing pre-defined fields.

FIG. 3 illustrates an example of objects represented as a main table 200. In the specific example shown in FIG. 3, the main table 200 (.account) represents a standard Account entity. As shown, main table 200 includes an organization ID (“org id”) column 201 and a table ID (e.g., “acc id” for account id) column 202 that acts as the primary key for table 200. Data table 200 also includes a plurality of data columns 203. Data table 200 may also include column 209 [add number to diagram] that stores the user ID of the user that owns or created the specific account that is stored in that row.

The org id column 201 is provided to distinguish among organizations using the multi-tenant account table 200. As shown, N different organizations have data stored in table 200. The org ids in column 201 are defined as Char(15) in an example implementation, but may include other data types. In one aspect, the first 3 characters of the org id is set to a predefined prefix, such as “00d”, although another subset of characters in the org id may be used to hold such a prefix if desired.

In the specific example of FIG. 3, where the table represents a standard entity, data columns 203 are the predefined data columns, or standard fields, that are provided to the various organizations that might use the table. In the standard Account entity example, such standard fields might include a name column, a site column, a number of employees column and others as would be useful for storing account-related information. Each of the data columns 203 is preferably defined to store a single data type per column.

II. Improving Queries for Use in an Environment

A. Problems of MTS in Ordinary Databases

Now, consider in a multi-tenant system one of the data columns 203 that is shared by many tenants and that has a large number of distinct values for most tenants, but a small number of distinct values for a specific tenant, e.g. org #2. For this latter tenant, a typical database optimizer will choose to use this overall-high-cardinality column in error because the optimizer is unaware that for this specific tenant the column is not selective.

In the case of table joins, the optimizer's decisions may be even more important as deciding which table to retrieve first can have a profound impact on overall query performance. Here again, by using system-wide aggregate statistics, a conventional query optimizer might choose a query plan that is incorrect or inefficient for a single tenant that does not conform to the “normal” average of the entire database.

As a specific example of the importance of table joins, consider a private sharing feature that allows groups defined within a particular tenant(s) to share information only among members of that group provided in certain embodiments. This private sharing feature allows a specific list of users to have access to privileged data, for example, such as specific accounts or opportunities. It is noteworthy that not all tenants will elect to enable private sharing for their implementations. Some tenants will elect public sharing paradigm instead. In a public sharing implementation, each user associated with a tenant sees every data row within the tenant's organization. Sharing tables, such as the Many-to-Many (MTM) physical table, e.g. table 400 of FIG. 4, for example are populated for private sharing implementations only and are joined in queries made by organizations using private sharing.

Table 400 of FIG. 4 illustrates an example storage mechanism for storing information by a tenant using a private sharing mechanism provided by an embodiment. It is noteworthy that table 400 is merely an illustrative example of one private sharing implementation and is not intended to limit embodiments to sharing only specific types or quantities of information. The MTM table 400 specifies that a user or group of users has access to a particular entity (e.g., account or opportunity) row. In the example MTM table 400 illustrated by FIG. 4, a particular organization assigned acc id “ood1” has two user group IDs, “Engineering” and “Bridge” defined. When displaying a list of all rows that the current user can see (possibly with a filter on the entity rows, such as the name of the account or the dollar amount of the opportunity) the query optimizer will choose between accessing this MTM table 400 from the user side (i.e., Engineering or Bridge) or the entity side (i.e., “ood1”) of the relationship.

If an entity filter is highly selective (for instance, a particular account name such as “XYZ Corp”) it will generally be more likely to provide a more efficient query by beginning the query access path from the account side. If, however, the entity is not filtered selectively, but a current user has access to a small amount of data, then the query optimizer should access rows in the MTM table 400 through the user side. Because in the above example. a conventional database system optimizer's native statistic methods may be insufficient to make this determination, since the native statistics will aggregate across multiple tenants and will not provide context into the current tenant's data, embodiments implementing private sharing provide mechanisms and methods for improving the original query prior to the query being submitted to the database.

It is noteworthy that, because of the wide range of business types, industries, and sizes potentially served by multi-tenant database systems-, the likelihood of data “skew” is greatly increased. That is, the statistical profile of the largest most complex tenants is likely to look very different from that of small or medium sized customers.

B. Embodiments that Provide “Hints” and Improved Queries Using Tenant-level Statistics

In embodiments implementing database systems provided by Oracle Inc., a company with headquarters in Redwood Shores, for example, override mechanisms are provided that affect the Oracle native query optimizer. The use of query “Hints” enable the ability to choose explicitly an improved query plan. For instance, an improved SQL statement might mention the explicit order of table joins, or explicit index names to use (rather than letting the optimizer choose automatically). Another mechanism for controlling the query plan explicitly is to re-write the query using equivalent but different SQL syntax. For instance, a single flat SQL statement can be re-written using a nested SELECT in the FROM clause of the outer query. Joins and semi-joins are sometimes inter-changeable. Anti-joins can be written using the MINUS operator, etc. All of these are examples of ways in which a programmatic SQL generator can alter the behavior of the query optimizer native to an underlying database by using contextual knowledge to change the query plan.

In certain aspects, a query optimizer native to a RDBMS, such as the query optimizer provided with the RDBMS by Oracle, may be configured or “tuned” by supplying appropriate “hints” to the native query optimizer. For example, when SQL is generated programmatically by the MTS, the tenant-level statistics are consulted and a dynamic decision is made as to the syntax of the query. As used herein the term tenant-level statistics is broadly defined as statistical quantities that, though they may mirror the underlying relational database statistics in many ways (for example, in one aspect they track the total number of distinct values for indexed columns), are kept on a per-tenant basis. In one embodiment, tenant level statistics may be stored in tables in tenant database storage areas 112. Similarly for important application functionality, such as the sharing feature, the MTS tracks the approximate number of rows to which each user has access and stores such statistics (e.g., tables stored in user storage areas 114 of database 108). Then, when a filtered sharing query arrives, the dynamically generated SQL includes the appropriate hints and structure to force a query plan that is improved.

In one aspect, metadata information about users and tenants/organizations and the data contained in entity rows for that tenant are tracked (e.g., relevant information and metadata stored to separate user-level and tenant-level data tables) in order to make choices about query access paths. These techniques can provide particular value for list-style queries such as reports. A variety of areas are targeted, a brief overview of some of these areas will next be given as an illustrative and non-limiting examples. In an embodiment, evaluation of a sharing model controls which users can see which records. These embodiments can distinguish between users that can see many rows in an organization (e.g., bosses) versus users who can see very few rows (e.g., lower level employees). In another embodiment, filters are selected based upon a determination which filters are the most selective for fields that contain enumerated lists of values (e.g., list of status values for an account, list of industries, list of states, etc.). In a further embodiment, the joining and tracking of specialized tables is made more efficient by using tenant-level statistics. In a yet further embodiment, a sharing model is combined with a proper choice of filters, e.g. which model or filter should lead the query and how should the other filters be organized in a query plan in order to improve the efficiency of the query. In a still yet further embodiment, tenant specific information may be used to improve a query. For example, if one filed is expected to be non-null for one tenant but not necessarily for all tenants, embodiments can generate different SQL for when the tenant's users filter on this field so that a query optimizer native to a database manager may benefit from the SQL generated in view of the field not being null in all cases for this tenant.

III. Improving the Search on Rows

A. Sharing Model

In embodiments, for each user in the system, an approximate count of the number of rows that the user can see is tracked for each entity type or organization that has a sharing model. This number of rows (as a percentage of the total number of entity rows for that organization) is used as a decision point by embodiments selecting between two different query paths. It has been determined empirically that users who can see most of the entity rows (e.g., bosses) benefit from a certain query structure, whereas users who can see a small percentage of the entity rows (e.g., lower level employees) benefit from a different query structure. Conventional approaches are not able to select between the two query paths without having an entirely different SQL provided via a programmatic decision.

In aspects, a query improver reads data from multi-tenant data tables and stores metadata (e.g., number of rows accessible per tenant or per user, or other metadata) to tenant-level tables or user-level tables in database 108. For example, a tenant-level metadata table might be stored to a tenant storage area 112 and a user-level table might be stored to a user storage area 114. In one aspect, the query improver includes a metadata generator that processes multi-tenant tables and produces tenant-level and user-level metadata tables, such as the tenant level metadata table 450 shown by FIG. 4. The query improver and metadata generator might execute in process space 110 or other process space. The query improver retrieves and processes the appropriate tables when preparing improved SQL queries. In other aspects, flags or tags may be implemented in the multi-tenant database tables to distinguish users and tenants.

The number of rows that are accessible by a tenant or user may be calculated based on the ownership of a row, which is tracked in column 209 of table 200. The ownership information can be entered when the row (e.g. account) is created or by other means and/or at other times depending upon implementation specific details. From this ownership data, metadata tables may be permanently created or calculated dynamically upon login of a user. Using such metadata, a user query may be improved prior to submission to an underlying database manager. If a user can see few rows, then a query may be improved by structuring the query to retrieve all of the data in those rows and then apply any desired filters to the data in the selected rows. For example, consider a query of the form: “Show me all accounts that I can see” in a private account sharing model. An example of a data model for sharing appears in FIG. 3 (middle table is sharing table, final table is the user/group “blowout” table which describes which users are contained in a group, or above a user in the role hierarchy (UG=User or Group)). According to one aspect, for a “lower level employee” user it is typically most advantageous to join these tables starting from the right, filtering on users Id to form a temporary result of the rows that can be seen. Because the user can not see many rows, this will yield a relatively selective path. An example query follows: select a.name “ACCOUNT.NAME”, from sales.account a, (select distinct s.account_id  from core.ug_blowout b, sales.acc_share s where s.organization_id = ? and b.organization_id = ? and b.users_id = ? and s.ug_id = b.ug_id and s.acc_access_level > 0) t, core.users u where (t.account_id = a.account_id) and (u.users_id = a.owner) and (a.deleted = ‘0’) and (a.organization_id = ?) and (u.organization_id = ?))

,Conversely for a “boss” user who can see most of the entity records in the organization, the query improver will select another way to access the data, e.g. by applying a selective filter on all rows of the desired tenant. If the metadata gathered for a boss (done by gathering up the owernship numbers for people beneath) indicates access to many rows, it is typically most advantageous to begin the query from the left and use a nested loop query plan onto the sharing table (acc_share), an example of which follows: select a.name “ACCOUNT.NAME”, from sales.account a, core.users u where (u.users_id = a owner) and (a.deleted = ‘0’) and (a.organization_id = ?) and (exists (select 1 from core.ug_blowout b, sales.acc_share s where s.organization_id = ? and b.organization_id = ? and b.users_id = ? and s.ug_id = b.ug_id and s.acc_access_level > 0 and s.account_id = a.account_id)) and (u.organization_id = ?)

Note that this query in general runs in relatively constant (reasonable) time for all users in an organization. It may not be particularly fast since it must look at all top-level entity records, but it is suitable for a boss who can in fact see most records. The first “lower level employee” query runs much faster for users who in fact can not see many records, but it may run much slower for bosses who can see all records. This, again, is why it is desirable to have an accurate decision between the two paths.

In order to keep the metadata current, the percentage of rows that each and every user can see can be tracked. In one aspect, there are three ways in which a user might gain access to data in a private security model:

(1) Rows owned by the user or users below the user in the role hierarchy;

(2) Rows that are shared via sharing rules to a group to which this user belongs; and

(3) Rows that are shared via manual/team sharing to this user (possibly via a group).

In one aspect, the total number of rows for each entity type for each organization is tracked (this is useful for any of the strategies above). Also, the total number of rows owned by each user in a metadata table is tracked.

Because (1) and (2) can be important reasons for tracking why a user has access to entity records in some implementations, (this might be known empirically from how organizations use the system) the information needed to calculate the number of rows a user can see, at least approximately, can be determined from the role hierarchy metadata table 475 of FIG. 4 for example, in conjunction with the metadata table 450. These metadata tables can be used to determine the number of records owned by a user or his subordinates.

The sharing rule metadata, such as tables 400 or 450, can also be used along with the group definition metadata, such as table 425, to calculate the total number of rows visible for a given user via sharing rules. For example, tables 400 and 425 may be joined from the right so that all of the groups to which a user belongs is determined. The number of rows seen by each of these groups may then be obtained from table 450 and added together. Table 450 may be calculated from table 400 for each organization. It will be appreciated by those skilled in the art that, while these methods of determining users from metadata tables illustrated by FIG. 4 are specific to a particular tenant in a multi-tenant database, other metadata tables may be included to hold metadata for other tenants.

It is also noteworthy that, for the purpose of the heuristic decision between “boss” and “lower level employee,” the sum of these two values is sufficiently close to the true value even though these two sets may overlap.

In one aspect, the use of metadata tables such as the metadata tables illustrated by FIG. 4 (which are generally much smaller than the actual entity tables which might have millions of rows) ensures that the calculation of visible rows will itself not require much time. In one aspect, the decision to use metadata tables to perform this calculation is itself cached in a user-information data structure kept by running application servers 100, e.g., with a timeout value. In this manner, even though the calculation itself may be relatively lightweight, it is only performed periodically while a user is logged in.

Besides user group ID, Table 450 could have a tenant, division, group or other type of identification. Thus, each one of the tables in an example embodiment illustrated by FIG. 4 may hold tenant-level, user-level, or other level metadata based on a type of identification.

B. Division Predicate Pushing

In embodiments, a feature known as “Divisions” enables partitioning one tenant into “sub-tenants.” This allows splitting a large entity, such as Accounts, into many smaller ones. Users can choose to work within one division at a time. Most of the large tables in the physical data model have an index on a physical division column. Whenever a user filters their top-level operation on the division field, the query improver “pushes this predicate” (i.e., carries forward this filter) not only into the top-level entity but also into any adjunct tables (most notably the custom field and sharing tables) that are known to share this same semantic division value. This can cause the entire query to run much faster for various different types of filter predicates and users because the number of rows seen by a user performing the search will be reduced to the number of rows in the division.

IV. Improving the Search on Columns in a MTS (Filter Choice)

A typical end user report execution includes a set of displayed columns from multiple tables along with a set of filter conditions. A typical report might join between three (3) and seven (7) (or more) main tables with filtering possibly occurring on one or more of these tables. Given the number of joins, the number of predicate filters that might occur in a query can be quite high. Thus, proper choices of which columns to use to join the tables can increase efficiency significantly over conventional database query optimizers that compute selectivity based on all indexes in the physical database, a method that is practically certain to succumb to data skew from uneven data distribution among tenants. In addition, certain filters, such as the sharing filter discussed above (which can take the form of an additional join or a nested sub-query), should be applied to assure that the end user only sees data to which the end user has been given access.

A. Choosing a Column to Improve a Query

The optimization of query searches may be prioritized based on the selectivity of the filters used on the columns. In one embodiment, the column with the highest cardinality is selected to lead the query. Each filter predicate may be examined in order from the main table to adjunct tables in search of the one with the highest cardinality. The search may be only for the one with the highest cardinality or all of the filter predicates may be put in order. The cardinality may be tracked in the following ways.

Information about enumerated “picklist” fields (those fields that are known to contain a small list of possible values) can be tracked in one aspect. For example, a cardinality for each picklist data column may be kept on a tenant, division, and/or custom entity basis. Examples of “picklist” fields include the list of priority values for a task and the list of industries for an account. These fields are often used as filters for executive reporting and data rollup reports. In addition to the values themselves, the approximate number of times each value appears in the actual entity table for that organization (tenant) can be tracked in the metadata. When a user provides a filter value such that the value appears infrequently for that organization, the overall query is preferably driven from that table and possibly from an index on that column, if such as index exists. For picklist fields, the metadata tracked and stored does not need to reflect the exact number of occurrences for each value, a reasonable estimate is sufficient and may be used in embodiments. Values missing from the metadata either do not occur at all, or occur infrequently, and thus such metadata can make good filters.

In one aspect, when a user runs a report with N filters, each filter is evaluated for expected selectivity. If, for example, the user filters on “California” and “Florida” from a list of states and it is known that these values represent, respectively, five percent (5%) and two percent (2%) of the overall rows, then it is assumed that the filter has a seven percent (7%) selectivity. Similarly if a Boolean field has 95% true values, then filtering on false appears attractive as a filter, whereas filtering on “Male” from a random list of people would not be very selective, since 50% reduction would not make a good filter condition.

The selectivity of the sharing condition is also considered in one aspect. For a user with very low (perhaps two percent (2%)) visibility, the sharing filter might prove to be the best starting point and therefore the query optimizer native to the RDBMS is instructed to begin with the filter, rather than one of the main entity tables such as, e.g., Account or Contact tables.

In one aspect, other filter types may be incorporated into the query improvement mechanism, in addition to semantic knowledge about the application. For example, if an organization has imported all opportunities for the last three (3) years, and a user report filters on “all opportunities that closed in the last week” then this is likely to represent a selective filter. The presence of custom field indexes (e.g., a certain set of columns that administrators can choose to place into a B-tree indexed custom field columns into these heuristic decisions) are also factored in one aspect, as will be discussed below.

In one aspect, a query is only hinted to the RDMS optimizer if it is likely that a particular starting table will yield a selective path. All other tables would then be joined via nested loops. Importantly, embodiments can make tenant-level data decisions based on tenant-level metadata and user-level data decisions based on user-level metadata. Embodiments also can take into account application-level concepts such as sharing that are beyond the generic nature of the underlying RBDMS.

B. Using Denormalized Data Structures to Improve Queries

Embodiments can form an improved query that includes one or more references to a denormalized data structure that enables accessing names in the database stored in a name table, enabling customers to build indexes by copying denormalized field values into a universal indexed table, and using a “reverse index” to locate a foreign key from a parent to a child foreign key table. As used herein the term denormalized index table is defined as any indexed tables where instead of adding an Oracle—type index on a column of the regular multi-tenant wide table, the contents of that column are copied into a new separate narrow table, prior to adding Oracle—type indexes on the narrow table. Examples include SNL, CIV, and CFKV tables described herein below. To take advantage of the index, an SQL query is generated or rewritten to join to the narrow table. Denormalized table techniques can provide embodiments having the advantages of: (1) selectively copy the contents for one tenant but not for another tenant, so that resources are not wasted maintaining information if the tenant doesn't need indexing; (2) because there are so many columns in the regular multi-tenant wide table, it is not practical to add Oracle—type indexes to each and every one of them, however each tenant will likely need different columns to be indexed, and the requirements may change over time; (3) in the CIV table, Oracle—type indexes for string, number, and date contents, are separated out in order to support querying, filtering, and sorting of the different content types. Embodiments using denormalized data structures may create one or more the special purpose tables that may be joined and tracked with tenant-level metadata as will next be described with reference to examples.

In one embodiment and by way of example, metadata may be collected periodically on the core.custom_index_value and core.custom_foreign_key_value tables and -high cardinality values (histograms on a per-organization, per-field basis) stored. When a user filters for these values, the cardinality of the result can be estimated quite accurately in many cases based upon the stored metadata.

1. Search Name Lookup

FIG. 5 shows a search name lookup (SNL) table according to an embodiment and by way of example. It is noteworthy that there are a wide variety of formats that can be used to organize the information in search name lookup table 500 and that there may be many different forms for the names. The names could be by first, last, both, reversed order of both (i.e. last name first) for example, just to name a few.

Accordingly, to address the issue of disparate name formats and to insure that names are unique across multiple tenants, embodiments include a universal name table that stores all names known to the database. Any filter predicate against one of the known “name” columns (such as Account Name) must be routed first against this table in order to assure that the name is being searched in the correct tenant data area so that the search can proceed properly. This universal name table is made available to the RDBMS native query optimizer with any query by providing the native query optimizer with access to table 500 when an improved query is passed to the native query optimizer in order to insure that names are kept unique across multiple tenants in the database.

2. Custom Index Values

Each organization may add or define custom fields for inclusion in a custom field table. These custom fields may be inserted into any number of columns of the custom field. Custom fields for multiple tenants are stored in a single field within a custom field data structure, and this single field may contain different data types for each tenant.

FIG. 6 illustrates an example custom field table in an embodiment. Custom field table 610 similarly includes an org id column 611, a table id column 612 and plurality of data columns 613. As above, acc id column 612 acts as the primary key for table 610 and can include the same values as acc id column 202 of table 200. In the specific example shown, there is a plurality data columns 613 labeled as val0, val1 . . . val249. It is appreciated that any other number may be used as desired, such as, for example, 10 or 100. The account ID 612 may be used as the primary key values as they are globally unique.

In other databases the columns of a table are not indexed as this would create too much data and eventually not perform an efficient search. A benefit may be had in indexing some columns. The problem is how to know a priori, which columns will be useful to index. This would be particularly hard in a multi-tenant context since a column may be useful to index for one tenant and not useful for another tenant. A solution is to let the tenants decide which field is indexed for its part of the database. A tenant can add a name to this custom index value and decide for which column of information is indexed.

Now consider the problem of indexing the data in these custom field columns (e.g., columns 613) to allow for fast retrieval. For example, users expect to filter on date values as dates and numeric values as numbers. In order for these filters to work efficiently, given the expressions above used to convert their values, however, it would be necessary to place a functional index (e.g., an Oracle® DB functional index) on each organization's slice of the data in a given custom field column. This may not be possible, however, because the underlying RDBMS may not permit one physical column to contain data in multiple formats. For example, if one tries to create an index on the TO_DATE or TO_NUMBER expressions above, an error would result since other textual values in that physical column would not conform to the desired format.

For the reasons listed above, such “Indexed Custom Fields” are implemented in one embodiment by storing data in a separate set of indexed columns. According to one embodiment, a plurality of additional index columns is provided to allow for indexing custom fields. When a custom field is flagged for indexing by a database administrator, one of the index columns is allocated to that flagged column. Data from the flagged column is copied to the allocated index column. The data is stored in the index column in a format that facilitates searching, e.g., for dates and strings. For example, the YYYYMMDD is itself a searchable format as strings in that format can be compared lexically using normal string comparison.

FIG. 7 illustrates an example of a custom object represented as a custom field table 710 including physical index columns 720 according to one embodiment. In one aspect, each custom field data table contains multiple (e.g., 10, 100, 250, etc.) physically indexed columns 720, e.g., using standard B*Tree indexes. In an example with ten (10) indexed columns, an administrator can therefore designate up to ten (10) custom fields, of string or date types, to be indexed. When a custom field is flagged for indexing, the data in the original column (which is still maintained to display the unmodified format to the user when necessary) is copied to one of these indexed columns. For example, as shown in FIG. 7, custom data field “val0” was flagged by the system administrator for organization 1 as an indexed custom column. Data from this flagged column is copied to the index column “ival0”. Similarly, custom data field “val1” was flagged by the system administrator for organization 2 as an indexed custom column, and the data from this flagged column is copied to index column “ival0”. At some later time, the system administrator for organization 2 may have flagged another custom field column and the data for this column is copied to another index column (e.g., column “val0” data copied to column “ival1” as shown in FIG. 7). In one aspect, similar to the custom fields, the lowest numbered index columns are used or filled first.

In order to enable customers to build their own indexes, denormalized field values are copied into a universal indexed table at indexed columns 720 (after normalizing text values for case-folded and case-insensitive searching, etc). These custom field values are made available to the RDBMS native query optimizer with any query that references at least one of the custom fields by providing the native query optimizer with access to table 720 when the custom fields are filtered.

3. Custom Foreign Key Value

Within the custom fields of a single tenant, a custom entity may also be created. Each custom entity type may have a different data type stored in the same physical column. The account ID may be used as the primary key values as they globally unique, but also include an object-specific identifier which may be re-used among different entities.

FIG. 8 illustrates an example of a custom entity object in an embodiment. Custom entity object 800 includes a custom table 810 for an organization. As shown, this organization (identified by “00d1” in org id column 801) has defined a custom object 810 to represent Assets. The definition of the custom fields that comprise the Assets object 810 is stored to metadata, e.g., in columns 613 of table 610 (FIG. 6). Assets object 810 has been allocated the prefix “a02” for the custom entity id. Also, as shown, the organization identified by “ood1” has defined another custom object, e.g., identified by prefix “a01” in custom entity id column 802. A separate column may be provided in table 800 to store the prefixes (e.g., “a01”) of the various objects stored in table 800. Assets object 810 has been defined with a custom foreign key column and various data columns. The custom foreign key (FK) column is allocated to the “Val0” column, whereas data fields for “asset name”, “asset value”, “asset depreciation type” and “asset replacement date” have been allocated to columns “Val1” to “Val4”, respectively. In this example, the data types for these fields are text, number, picklist and date, respectively.

Assets object 810 is a child custom object of Account object 500. The custom foreign key column connects each row in object 810 to its parent account (in these examples, Account object 500 has been allocated a prefix of “001” for its table id). For example, the foreign key value “001 . . . 9” connects to the row in table 500 for account name “DELL”. Similarly, the foreign key values “001 . . . 8” and “001 . . . 10” connect to the rows in table 500 for account names “IBM” and “APPLE”, respectively. Also, as shown, XYZ Corp. (identified by “00dN” in org id column 801) has defined a custom object to suit their business needs, which custom object is also stored in table 800. As such, any given data column 803 may contain mixed data types depending on the definitions of the various custom objects stored in table 800.

In order to allow an arbitrary number of foreign keys for each tenant embodiments can use a reverse index table 550 of FIG. 5 to “reverse index” the foreign key from parent to child. Without joining in this table no query can ever efficiently find all child rows for a particular parent, for one of the custom foreign keys. If an object has many children, it is hard to find all of the children efficiently. Thus, embodiments provide a table 550 of FIG. 5 that lists all of the children for a particular parent.

C. Rearranging Query (Query Normalization)

In an embodiment and by way of example, query normalization (combining disjunctions and conjunctions of similar predicates) is performed into a format that increases the ability for the above optimizations to be effective. For instance, a parallel list of OR equality conditions on one field can be combined into a single IN condition (before consulting our metadata) and a parallel set of AND conditions will be intersected into a single filter.

V. Combining Sharing and Filters

Armed with the knowledge about the selectivity of the current filter predicates (and other contextual information) along with knowledge of where the current user fits into the sharing model, embodiments can make intelligent decisions about evaluation sharing in one of the following three ways.

i. If the query leads with a small number of entity rows (either because the query starts with a set of Ids, such as when displaying search results, or because it can be determined that a filter predicate is selective) then the improved query is generated to include sharing evaluated with a nested loop query plan leading with the entity ids

ii. Otherwise if the user cannot see many rows then the improved query is generated to include leading with the sharing tables and accessing the main entity via the owner field (which is indexed)

iii. Otherwise if the user is not an admin then the improved query is generated to include a hash semi-join against the full set of rows that the user can see.

In addition to enabling faster queries by having an additional indexable field, extra fields may have associated metadata. The metadata can be updated when the field is input, for example. With these metadata, a user query may be improved. For example, ownership metadata may be determined for how many rows are visible to a user. Thus, if a user can see few rows then a query may be optimized by retrieving all of the data in those rows. However, if the metadata gathered for a boss (done by gathering up the owernship numbers for people beneath) show access to many rows then the query improver will select another method for accessing the data.

VI. Maintaining Metadata

A. Sharing Model

In an aspect, metadata, including statistics, is tracked for user and organization quotas. In some aspects, such information is tracked periodically (e.g., on a scheduled basis—during off-peak hours, amortizing the work over multiple days), wherein the number of visible rows for each user is calculated exactly or approximately, or before every Nth query (e.g., every 10^(th) query) by a user, that user's visibility is calculated explicitly and then that statistic is used until it is again calculated (here it is assumed that users do not change very often from one strategy to another). In yet a further aspect, whenever an unconstrained query is run, the number of visible rows is remembered and that number is used until the user runs the next unconstrained query.

When data is created, certain statistics are entered into the table, such as ownership, tenant, user-id, and other statistics mentioned herein. This may be termed being done at the data manipulation layer (DML) time. In this manner, such fields may be used to search based on the owner of the data. This information may also then be used to calculate how many other users may be able to view the information based on a hierarchical user structure within a tenant. For example, a manager will be able to see the accounts that people underneath have created. Accordingly, embodiments can provide a payoff in faster queries with little extra work when the data fields are being created.

B. Filter Selection

In one embodiment, each update or insert into an entity table passes through an application server 100. Therefore as the data is being processed in Java™ (Java is a trademark of Sun Microsystems, Inc.) counters are incremented and decremented for individual picklist values. Inserts increment a counter, while updates to a different value decrement a counter for the old value and increment a counter for the new value. Since these statistics do not need to be exact, the statistics metadata need not be updated with each and every database insert or update (which might affect performance). Rather, in one aspect, an in-memory cache server (which already contains the metadata for valid picklist values) is augmented with the counters for these values, with the ability to update the database values periodically to persist the changes. An example of such a cache server can be found in U.S. patent application Ser. No. 10/418,961, filed Apr. 17, 2003, titled “Java Object Cache Server for Databases”, the contents of which are hereby incorporated by reference in its entirety.

For row deletion, the data need not pass through the application server 100. However, all main entities are soft-deleted (with a modification stamp), meaning the rows are left in the database for possible un-deletion. Therefore, an asynchronous process is used to update the statistics when rows are deleted and un-deleted since it is known which rows have been touched since the last running of that process.

VII. Pre-Fetch Queries

In some instances, metadata may be insufficient to determine whether a particular filter is selective. In such cases, an embodiment can issue a “pre-fetch” query looking for the number of rows directly in the database that match the filter (it will only do this if the answer to that question can be found easily, i.e. a simple fast query can be used as the pre-fetch, and a ROWNUM limit may be used to “exit early”). Normally issuing such a pre-query, possibly for many separate filters, would be considered too expensive in the general case, however, contextual knowledge can facilitate the ability to decide that the up-front cost is worth the effort e.g., before running a possibly very computationally expensive Management Report. In addition, embodments may keep row counts for each entity for each tenant and factor this information into the decision of whether to pre-fetch.

While some metadata may be useful in giving an improved query, the improvement may only be in removing some possible retrieval paths while other retrieval paths may be seen as possibly being equivalent. When a retrieval path is seen as equivalent, the a pre-query fetch may be used. A pre-query fetch looks at how many entries are in a specific retrieval path. If there are too many entries then that path is seen as not beneficial. This is different from other methods that look at random entries to gather metadata. These methods do not look at the results of an actual search, plus metadata from such a compilation will not be as directed since the actual serach metadata will use the concepts such as ownership, tenant, and user-id to gather the metadata for the pre-query fetch and utilize the metadata in the most efficeint manner given the structure of these known fields.

VIII. User-Specific Sorting

User-specified sorting (some sorting logic is semantically rich). For instance sorting on multi-currency fields, i.e. a field supporting more than one currency (dollar, yen, euro, etc.) or sorting on translatable picklist fields, i.e., a picklist field able to be translated into various values, or sorting on special application fields such as ActivityDate which is time zone and Calendar specific, all require detailed knowledge of application design and functionality.

In an embodiment, the appropriate joins or inline views can be injected to achieve the correct application functionality, and may include augmentation by contextual information retrieved from other areas of the database (for instance in the case of picklist translation, embodiments may consult picklist Java-objects and create an Oracle in-memory array for hash joining).

IX. Row Limits

Row limits—most UI queries only need to display a small number of rows at a time, which can allow for SQL optimizations.

Embodiments can employ such techniques to enable critical query re-writing that is not possible at the underlying RBDMS optimizer by, for example determining that certain joins can be deferred until after the ROWNUM limit has been placed early in the query. This can be detected from contextual information because semantically an application employing the MTS will not change the results of a query with a particular join (for instance, joining to the user table to get the name from a user ID will never change the rows in the main query).

While the invention has been described by way of example and in terms of the specific embodiments, it is to be understood that the invention is not limited to the disclosed embodiments. To the contrary, it is intended to cover various modifications and similar arrangements as would be apparent to those skilled in the art. Therefore, the scope of the appended claims should be accorded the broadest interpretation so as to encompass all such modifications and similar arrangements. 

1. A method of improving a query in a database, the method comprising: receiving a query directed to the database, the database storing data specific to each one of a plurality of tenants such that at least two of the tenants store at least a portion of data specific to the at least two tenants in a common table within the database and wherein each tenant is permitted access only to data associated with that tenant; and providing, in place of the query, an improved query, the improved query determined based at least in part upon the query received and a set of contextual information, the contextual information describing a characteristic of the data specific to at least one of a plurality of tenants, thereby enabling constructing the improved query to be aware of the data specific to a tenant to which the improved query will be addressed.
 2. The method of claim 1, wherein receiving a query directed to the database includes: intercepting a query directed to an optimizer integrated with the database prior to the query reaching an optimizer integrated with the database.
 3. The method of claim 1, wherein providing, in place of the query, an improved query includes: selecting a set of contextual information applicable to the query from a plurality of sets of contextual information applicable to the plurality of tenants in the database.
 4. The method of claim 3, wherein selecting a set of contextual information applicable to the query from a plurality of sets of contextual information applicable to the plurality of tenants in the database includes: selecting at least one from a plurality of row level sharing rules that prevent a user associated with a particular tenant from seeing data in the database associated with the particular tenant to which the user is not permitted to access; and creating the improved query so that the improved query indicates to a database how to search the data in the database that the user is permitted to search in a way that is likely to be improved over a way that the query received would have been searched in the database.
 5. The method of claim 1, wherein providing, in place of the query, an improved query includes: determining, based at least in part upon the query received and the set of contextual information, the improved query so that the improved indicates to a database how to search the data in the database in a way that is likely to be improved over a way that the query received would have been searched in the database.
 6. The method of claim 1, wherein providing, in place of the query, an improved query includes: determining, based at least in part upon the query received and the set of contextual information, the improved query, an improved query that includes at least one reference to a denormalized data structure, the denormalized structure permitting at least one of: accessing names in the database stored in a name table, enabling customers to build indexes by copying denormalized field values into a universal indexed table, and using a “reverse index” to locate a foreign key from a parent to a child foreign key table.
 7. The method of claim 1, wherein providing, in place of the query, an improved query, the improved query determined based at least in part upon the query received and a set of contextual information, the contextual information describing a characteristic of the data specific to one of the plurality of tenants includes: improving the query received by rewriting or generating different SQL based at least in part upon at least one of a tenant-level sharing model, statistics and metadata.
 8. The method of claim 7, wherein improving the query received by rewriting or generating different SQL based at least in part upon at least one of a tenant-level sharing model, statistics and metadata further includes: determining a selectivity of a predicate filter on a column of a table based on only data for a single tenant.
 9. The method of claim 8, wherein a characteristic of data for a single tenant defines a cardinality of a column only within a single tenant.
 10. The method of claim 7, wherein improving the query received by rewriting or generating different SQL based at least in part upon at least one of a tenant-level sharing model, statistics and metadata includes: copying values of an indexed column into a separate denormalized-indexed table and wherein the denormalized-indexed table is separately indexed natively by a relational database management system (RDBMS).
 11. The method of claim 7, wherein improving the query received by rewriting or generating different SQL based at least in part upon a tenant-level sharing model, statistics and metadata includes: determining whether to copy into the denormalized-indexed table is based at least in part on a tenant-specific criterion.
 12. The method of claim 7, wherein improving the query received by rewriting or generating different SQL based at least in part upon a tenant-level sharing model, statistics and metadata includes: rewriting or generating the improved query with an extra join in the denormalized-indexed table.
 13. The method of claim 7, wherein improving the query received by rewriting or generating different SQL based at least in part upon a tenant-level sharing model, statistics and metadata includes: determining whether to make an extra join to the denormalized-indexed tables is based on at least one of: a set of tenant-level statistics, a set of cardinalities, a set of selectivities and metadata.
 14. The method of claim 1, wherein providing, in place of the query, an improved query further includes: rewriting or generating SQL of the received query to provide a hint in the improved query, wherein the hint instructs a relational database management system to lead the query with a table determined to be likely to produce improved query results when a user has access to only a small percentage subset of rows in a portion of the database associated with the tenant.
 15. The method of claim 1, wherein providing, in place of the query, an improved query further includes: rewriting or generating SQL of the received query to provide an improved query that instructs a relational database management system to perform a hash semi-join against a full set of rows that are visible to a user when the user is not an admin.
 16. The method of claim 1, wherein providing, in place of the query, an improved query further includes: rewriting or generating SQL of the received query to provide an improved query that leads with an entity id and performs a nested loop to the sharing table when the query leads with a small number of entity rows.
 17. The method of claim 1, wherein providing, in place of the query, an improved query further includes: providing the improved query to an optimizer integrated with the database in place of the query directed to the database.
 18. The method of claim 1, wherein providing, in place of the query, an improved query further includes: determining a number of rows that are accessible to a user, including at least one of: (a) determining the number of accessible rows is limited to a search of a specific tenant; (b) determining the number of accessible rows is limited to a search of a division (subset) of a tenant. (c) accessing a sharing table that lists the users or user groups that may access the row, wherein sharing table is determined based on an ownership column associated with a row; and (e) accessing a role hierarchy metadata table that lists the number of people below a user.
 19. The method of claim 1, further comprising: estimating whether a filter incorporated into the query is selective by performing a pre-fetch query, different from the query, wherein the pre-fetch query includes: identifying a number of rows that match a filter; and discontinuing the pre-fetch query and classifying the filter as non-selective when the number of rows exceeds a threshold limit.
 20. A method for transmitting code to improve a query in a database over a machine accessible transmission medium, the method including: transmitting code that causes one or more processors to receive a query directed to the database, the database storing data specific to each one of a plurality of tenants such that at least two of the tenants store at least a portion of data specific to the at least two tenants in a common table within the database; and transmitting code that causes one or more processors to provide, in place of the query, an improved query, the improved query determined based at least in part upon the query received and a set of contextual information, the contextual information describing a characteristic of the data specific to at least one of a plurality of tenants, thereby enabling constructing the improved query to be aware of the data specific to a tenant to which the improved query will be addressed. 